#include "upgradehmiexedlg.h"
#include "ui_upgradehmiexedlg.h"
#include "../CommunicateDelegate/globaldata.h"
#include <qfiledialog.h>
#include <qmessagebox.h>
#include "hmicommondefine.h"
#include "instructionEnum.h"



UpgradeHmiExeDlg::UpgradeHmiExeDlg(int styleId,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UpgradeHmiExeDlg)
{
    ui->setupUi(this);
    move(0,0);
    initialDisplayStyle(styleId);
//#ifdef BUILD_ARM
//    setGeometry(70,128,460,595);
//#endif


}

void UpgradeHmiExeDlg::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);
        break;
    default:
        break;
    }
}

UpgradeHmiExeDlg::~UpgradeHmiExeDlg()
{
    delete ui;
}

void UpgradeHmiExeDlg::on_pushButton_selectfile_clicked()
{
    int index=ui->localDir_ComBox->currentIndex();
    QString localFilePath;
    if(0==index)
    {
        localFilePath=D_HMI_PENDENT_UDISK_PATH;

    }
    else if(1==index)
    {
        localFilePath=D_HMI_PENDENT_UDISK2_PATH;

    }
    else if(2==index)
    {
        localFilePath=D_HMI_PENDENT_UDISK3_PATH;

    }

    //定义文件对话框类
   QFileDialog *fileDialog = new QFileDialog(this);
   fileDialog->resize(400,500);
   //定义文件对话框标题
   fileDialog->setWindowTitle(tr("选择文件"));
   //设置默认文件路径
   fileDialog->setDirectory(localFilePath);
   //设置文件过滤器
   fileDialog->setNameFilter(tr("zip(*.*)"));
   //设置可以选择多个文件,默认为只能选择一个文件QFileDialog::ExistingFiles
   fileDialog->setFileMode(QFileDialog::ExistingFiles);
   //设置视图模式
   fileDialog->setViewMode(QFileDialog::Detail);
   //打印所有选择的文件的路径
   QStringList fileNames;
   if(fileDialog->exec())
   {
       fileNames = fileDialog->selectedFiles();
   }
    qDebug()<<fileNames;
    if(fileNames.size()>0)
    {
        ui->lineEdit_filepath->setText(fileNames[0]);
    }
}

void UpgradeHmiExeDlg::on_pushButton_upgrade_clicked()
{
    QString systemCommand;
    QString filePath=ui->lineEdit_filepath->text();
    systemCommand="cp  "+filePath+" /agvwork";
    qDebug()<<systemCommand;
    ui->label_info->setText(tr("正在复制文件，请稍等！！！"));
    system(systemCommand.toLatin1().data());
    QMessageBox::information(NULL, "提示", "请手动关机重启完成升级！！！", QMessageBox::Yes );
//    system("killall heart.sh");//把心跳关闭
//    system("killall hmiMain ;reboot ");reboot后，触摸屏没有反映。

}

void UpgradeHmiExeDlg::on_pushButton_hide_clicked()
{
    hide();
}

void UpgradeHmiExeDlg::on_pushButton_restore_original_clicked()
{
    QMessageBox message(QMessageBox::NoIcon, "note", "确定要ＨＭＩ恢复出厂状态吗，旧ＨＭＩ信息将会丢失?"
                        , QMessageBox::Yes | QMessageBox::No, NULL);
    if(message.exec() == QMessageBox::Yes)
    {
        QString systemCommand;
        QString filePath="/armPendentHmiPackage.zip";
        systemCommand="cp  "+filePath+" /agvwork";
        qDebug()<<systemCommand;
        ui->label_info->setText(tr("正在从/armPendentHmiPackage.zip恢复文件，请稍等！！！"));
        system(systemCommand.toLatin1().data());
        QMessageBox::information(NULL, "提示", "请手动关机重启完成恢复ＨＭＩ出厂状态！！！", QMessageBox::Yes );
    }
}
